package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.AIReadConverter;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationMapper;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationOptionMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocAISituationCommand;
import com.cskaoyan.wordmemorize.service.AIReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AIReadServiceImpl extends ServiceImpl<UserVocAISituationMapper, UserVocAISituationDO> implements AIReadService {
    @Autowired
    UserVocAISituationMapper userVocAISituationMapper;

    @Autowired
    UserVocAISituationOptionMapper userVocAISituationOptionMapper;

    @Autowired
    AIReadConverter aiReadConverter;


    @Override
    public void addAISituation(UserVocAISituationCommand command) {
        if (command == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        UserVocAISituationDO userVocAISituationDO = aiReadConverter.userVocAISituationCommandToDO(command);
        save(userVocAISituationDO);

    }

    @Override
    public void updateAISituation(UserVocAISituationCommand command) {
        if (command == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        UserVocAISituationDO userVocAISituationDO = aiReadConverter.userVocAISituationCommandToDO(command);
        saveOrUpdate(userVocAISituationDO);

    }

    @Override
    public void deleteAISituation(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        removeById(id);
    }

    @Override
    public void deleteAISituationOption(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        userVocAISituationOptionMapper.deleteById(id);
    }

    @Override
    public void batchDeleteAISituationOption(List<Long> ids) {
        if (ids.isEmpty()) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        removeByIds(ids);
    }

    @Override
    public PageDTO<UserVocAISituationDTO> getAISituationList(PageRequest pageRequest) {
        if (pageRequest == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        Integer pageNum = pageRequest.getPageNum();
        Integer count = pageRequest.getCount();
        String keyword = pageRequest.getKeyword();
        Page<UserVocAISituationDO> vocAISituationDOPage = new Page<>(pageNum, count);

        LambdaQueryWrapper<UserVocAISituationDO> wrapper = new LambdaQueryWrapper<UserVocAISituationDO>()
                .like(keyword != null, UserVocAISituationDO::getName, keyword);
        Page<UserVocAISituationDO> page = this.page(vocAISituationDOPage, wrapper);

        List<UserVocAISituationDO> records = page.getRecords();
        long total = page.getTotal();

        return aiReadConverter.toUserVocAISituationPage(records, total);


    }
}
